package com.hhit.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hhit.pojo.Items;
import com.hhit.service.ItemsService;

@Controller
public class ItemsController {
	@Autowired
	private ItemsService is;
	
	/**
	 * 查询所有记录
	 * @return
	 */
	@RequestMapping("/findAllItems")
	public @ResponseBody List<Items> findAllItems(){
		List<Items> alllist =is.findAll();
		return alllist;
	}
	/**
	 * 按照名字查询
	 * @param items
	 * @return
	 */
	@RequestMapping("/findNameItems")
	public  @ResponseBody List<Items> findNameItems(@RequestBody Items items){
		List<Items> namelist =is.findByName(items);
		return namelist;
	}
	/**
	 * 时间转换 ----------网上找的。。。
	 * @param binder
	 */
	@InitBinder  
	public void initBinder(WebDataBinder binder) {  
	    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");  
	    dateFormat.setLenient(false);  
	    binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));  
	}
	/**
	 * 新增items
	 * @param items
	 * @return
	 */
	@RequestMapping("/addNewItems")
	public String addNewItems(Items items) {
		is.save(items);
		return "itemsList";
	}
	
	/**
	 * 删除items
	 * @param items
	 * @return
	 */
	@RequestMapping("/deleteItems")
	public String deleteItems(Items items) {
		is.delete(items);
		return "itemsList";
	}
	
	/**
	 * 跳转到修改页面 顺便传递相应的值
	 * @param items
	 * @param md
	 * @return
	 */
	@RequestMapping("toChange")
	public String toChange(Items items,Model md) {
		Items it = is.findById(items);
		md.addAttribute("Items", it);
		return "update";
	}
	/**
	 * 修改信息
	 * @param items
	 * @return
	 */
	@RequestMapping("/updateItems")
	public String updateItems(Items items) {
		is.update(items);
		return "itemsList";
	}
}
